package com.yupi.yuso.designpattern.adapter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yuso.designpattern.register.DataSourceRegister;
import com.yupi.yuso.model.dto.picture.PictureQueryRequest;
import com.yupi.yuso.model.dto.search.SearchRequest;
import com.yupi.yuso.model.enums.SearchTypeEnum;
import com.yupi.yuso.model.vo.PictureVO;
import com.yupi.yuso.service.PictureService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author vvv
 * @date 2023-12-21 21 59
 * discription
 */
@Component
public class PictureDataSourceAdapter implements DataSourceTarget<PictureVO> {

    @Resource
    private PictureService pictureService;

    @PostConstruct
    public void registerInstance() {
        DataSourceRegister.SEARCH_MAP.put(SearchTypeEnum.PICTURE.getValue(), this);
    }

    @Override
    public Page<PictureVO> doSearch(SearchRequest searchRequest) {
        // 这个bean 复制的操作就是在做适配，pictureService.listPictureVOByPage要的是PictureQueryRequest，
        // 但是doSearch的入参是SearchRequest searchRequest
        PictureQueryRequest pictureQueryRequest
                = BeanUtil.copyProperties(searchRequest, PictureQueryRequest.class);
        Page<PictureVO> pictureVOPage = pictureService.listPictureVOByPage(pictureQueryRequest);
        return pictureVOPage;
    }
}
